<?php
namespace app\common\model;
use app\common\traits\Models;
use think\Model;
use think\Db;
use think\facade\{
    App,Request,Config
};
class AdminUser extends Model
{
    use Models;
    protected $readonly = [];
    protected $auto     = [];
    protected $insert   = ['reg_ip'];
    protected $update   = ['last_login_time','last_login_ip'];
/*数据完成=================================================================================*/
    protected function setRegIpAttr()
    {
        return Request::ip(1);
    }
    protected function setLastLoginIpAttr()
    {
        return Request::ip(1);
    }
    protected function setLastLoginTimeAttr()
    {
        return Request::time();
    }
//获取器====================================================================================//
    public function getCreateTimeAttr($value)
    {
        return com_time_format($value);
    }
    public function getUpdateTimeAttr($value)
    {
        return com_time_format($value);
    }
    public function getTypeAttr($value)
    {
        return c("type.admin_user_type",$value);
    }
/*模型关联=================================================================================*/
    public function adminGroup()
    {
        return $this->belongsToMany('adminGroup','admin_group_user','admin_group_id', 'admin_user_id');
    }

 //逻辑方法=================================================================================
    /**
     * 登录
     * @param  [type] $username [description]
     * @param  [type] $password [description]
     */
  	public function login($username, $password){
        /* 获取用户数据 */
        $user = $this::where("username",$username)->find();
        if (empty($user) || (int)$user->status !== 0) {
            session('js.name', 'username');
            $this->error = '用户不存在或被禁用';
            return false;
        }
        if($user->password != com_password($password)){
            $user->error_times += 1;
            $user->save();
            session('js.name', 'password');
            $this->error = '密码错误';
            return false;
        }
        $token = com_token($user->id);
        $user->token = $token;
        $user->last_login_ip = Request::ip(1);
        $user->last_login_time = time();
        $user->login_times += 1;
        $user->save();
        session('token',$token);
        return $token;
  	}

    /**
     * 删除
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function del($id){
        if(!is_array($id)) $id = [$id];
        foreach($id as $v){
            if(!$this->checkDel($v)) return false;
            $this::destroy(function($query)use($v){
                $query->where('id',$v);
            },true);
        }
        return true;
    }
    /**
     * 验证是否可删除
     * @param unknown $id  用户id
     * @return true(可以删除) or false;
     */
    public function checkDel($id){
       $rs = $this::get($id);
       if( $rs->getData('type')==1 ){
            $this->error = '超级管理员不可删除';
            return false;
       }
       return true;
    }
}
